package main

import (
	"fmt"
	"sync"
	"strings"
	"time"
)

var okex_markets = strings.Split("ltc_btc eth_btc etc_btc bch_btc btc_usdt eth_usdt ltc_usdt etc_usdt bch_usdt etc_eth bt1_btc bt2_btc btg_btc qtum_btc hsr_btc neo_btc gas_btc qtum_usdt hsr_usdt neo_usdt gas_usdt"," ")


func OkexMarketToRedis(name string, wg *sync.WaitGroup){
	defer wg.Done()

	url := "https://www.okex.com/api/v1/ticker.do?symbol=" + strings.ToLower(name)

	body := httpGet(url)

	key := "Okex_" + name + "_Latest"

	err := client.Set(key, body, 0).Err()
	if err != nil {
		fmt.Printf("Error: %s\n", err)
	}else{
		//fmt.Printf("Success write: %s\n", key)
	}

}

func OkexMarketsToRedis(inWg *sync.WaitGroup) error{
	defer inWg.Done()

	t := time.Now()
	// fmt.Printf("OkexMarketsToRedis begin.\n")


	var wg sync.WaitGroup

	for _, market := range okex_markets {
		wg.Add(1)
		//fmt.Printf("OkexMarketToRedis(%s) begin.\n", market)
		go OkexMarketToRedis(market, &wg)
	}
	
	wg.Wait()

	fmt.Printf("OkexMarketsToRedis duration: %s\n", time.Now().Sub(t))

	return nil
}
